package com.asdan.seededu.web.controller;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.front.service.RegistService;
import com.asdan.seededu.front.service.impl.RegistServiceImp;
import com.asdan.seededu.front.service.impl.RegisterValidateService;
import com.asdan.seededu.service.impl.UserServiceImpl;
import com.asdan.seededu.util.Check;
import com.asdan.seededu.util.MD5Util;
import com.asdan.seededu.util.ServiceException;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-user")
@RequestMapping("user")
public class RegistController extends Check {
	Check check = new Check();

	@RequestMapping(value = "/sturegist", method = RequestMethod.POST)
	public String Regist(String password, String confirm_password, String name,
			String schoolname, String grade, String email, String cellphone,
			String qq, Model model) {
		RegistService rs = new RegistServiceImp();
		/*
		 * if(password==null ||"".equals(password)){
		 * model.addAttribute("message", "") ; }
		 */

		if (rs.getUserByEmail(email)) {
			model.addAttribute("message", "增加失败，邮箱已注册！");
			return "redirect:/regist_Stu.jsp";
		}
		if (password.length() < 6) {
			model.addAttribute("message", "密码长度不能小于6！");
			return "redirect:/regist_Stu.jsp";
		}
		if (!password.equals(confirm_password)) {
			model.addAttribute("message", "两次密码不一致！");
			return "redirect:/regist_Stu.jsp";
		}

		if (Check.checkEmail(email)) {
			rs = new RegistServiceImp();
			User user = new User(password, name, schoolname, grade, email,
					cellphone, qq);
			user.setValidateCode(MD5Util.encode2hex(email));
			Boolean isadd = rs.stuRegist(user);
			if (isadd == true) {
				// 注册
				RegisterValidateService service = new RegisterValidateService();
				service.processregister(email);// 发邮箱激活
				model.addAttribute("message", "增加成功,请去邮箱验证,即可登录！");
				model.addAttribute("email", email);
				return "redirect:/message_Emailconfirm.jsp";
			} else {
				model.addAttribute("message", "增加失败，请重新输入！");
			}
		} else {
			model.addAttribute("message", "邮箱格式错误！");
		}
		return "redirect:/regist_Stu.jsp";
	}

	@RequestMapping(value = "/teacherregist", method = RequestMethod.POST)
	public String teaRegist(String password, String confirm_password,
			String name, String schoolname, String position, String email,
			String cellphone, String qq, Model model) {
		RegistService rs = new RegistServiceImp();

		if (rs.getUserByEmail(email)) {
			model.addAttribute("message", "增加失败，邮箱已注册！");
			return "redirect:/regist_Tea.jsp";
		}
		if (password != null && !"".equals(password) && password.length() < 6) {
			model.addAttribute("message", "密码长度不能小于6！");
			return "redirect:/regist_Tea.jsp";
		}
		if (cellphone != null && !"".equals(cellphone)
				&& cellphone.length() != 11) {
			model.addAttribute("message", "手机号不正确！");
			return "redirect:/regist_Tea.jsp";
		}
		if (!password.equals(confirm_password)) {
			model.addAttribute("message", "两次密码不一致！");
			return "redirect:/regist_Tea.jsp";
		}
		if (Check.checkEmail(email)) {
			rs = new RegistServiceImp();
			User user = new User(password, name, schoolname, position, email,
					cellphone, qq);
			user.setValidateCode(MD5Util.encode2hex(email));
			Boolean isadd = rs.teacherRegist(user);
			if (isadd == true) {
				RegisterValidateService service = new RegisterValidateService();
				service.processregister(email);// 发邮箱激活
				model.addAttribute("message", "增加成功！");
				model.addAttribute("email", email);
				return "redirect:/message_Emailconfirm.jsp";
			} else {
				model.addAttribute("message", "增加失败，请重新输入！");
			}
		} else {
			model.addAttribute("message", "邮箱格式错误！");
		}
		return "redirect:/regist_Tea.jsp";
	}

	/*
	 * @RequestMapping(value="/teaschoollist") public String getSchoolList(Model
	 * model ) { RegistService rs = new RegistServiceImp() ; List
	 * list=rs.getSchools(); model.addAttribute("list",list); return
	 * "redirect:/registStu.jsp"; }
	 * 
	 * @RequestMapping(value="/stuschoollist") public String
	 * getStuSchoolList(Model model ) { RegistService rs = new
	 * RegistServiceImp() ; List list=rs.getSchools();
	 * model.addAttribute("list",list); return "stuRegist"; }
	 */
	@RequestMapping(value = "/action")
	public String actionUser(String email, String validateCode, Model model,
			HttpSession session) {
		User user = new UserServiceImpl().getUser(email, 0);
		RegisterValidateService service = new RegisterValidateService();
		try {
			service.processActivate(email, validateCode);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 发邮箱激活
		session.setAttribute("userid", user.getUid() + "");
		session.setAttribute("username", user.getName() + "");
		return "redirect:/index.jsp";
	}

	@RequestMapping(value = "/reregisteremail")
	public String reregisteremail(String email, Model model) {
		RegisterValidateService service = new RegisterValidateService();
		service.processregister(email);// 发邮箱激活
		model.addAttribute("message", "增加成功,请去邮箱验证,即可登录！");
		model.addAttribute("email", email);
		return "redirect:/message_Emailconfirm.jsp";
	}

}
